#include "../Hdr/Texture.h"
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_opengl.h>

Eternal::Video::Texture::Texture()
{
	
}

Eternal::Video::Texture::~Texture()
{
	
}

void Eternal::Video::Texture::Load(const char *file)
{
	
	/*
		TODO:
			*Implement custom image loader
	*/
	
	/* Load the image via SDL */
	SDL_Surface *tmp = IMG_Load(file);
	SDL_Surface *surf = SDL_DisplayFormatAlpha(tmp);
	SDL_FreeSurface(tmp);
	
	/* Allocate a new texture */
	glEnable(GL_TEXTURE_2D);
	glGenTextures(1, &glTex);
	glBindTexture(GL_TEXTURE_2D, glTex);
	
	/* Texture parameters */
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	
	GLenum format = (surf->format->Rmask == 0xff) ? GL_RGBA : GL_BGRA;
	glTexImage2D(GL_TEXTURE_2D, 0, 4, surf->w, surf->h, 0, format, GL_UNSIGNED_BYTE, surf->pixels);
	
	SDL_FreeSurface(surf);
}
